package kuangzhan

import (
	"math/rand"
	"time"

	. "7qule.com/sd3/CrossServer/src/bll/kuangzhan/bll"
	. "7qule.com/sd3/CrossServer/src/bll/player/bll"
	"7qule.com/sd3/CrossServer/src/webServer"
	"7qule.com/sd3/rpcService/Common"
)

func init() {
	webServer.RegisterFunction(new(KuangzhanApi))
}

// 玩家请求接口模块
type KuangzhanApi struct {
}

//
// GetKuangzhan
// @Description:首页
// @receiver this
// @param playerId:用户id
// @return *webServer.ResponseObject:响应对象
//
func (this *KuangzhanApi) GetKuangzhan(playerId string) *webServer.ResponseObject {
	//获取相应信息
	responseObj := webServer.GetInitResponseObj()

	player, playerExist := GlobalPlayerBll.GetItem(playerId)
	if !playerExist {
		return responseObj.SetResultStatus(Common.ResultStatus_PlayerNoExist)
	}

	playerNode, playerHaveNode := GlobalKuangzhanBll.GetPlayerItem(player.Id)
	lid := BaseKuangzhanModelBll.GetSmallestItem()
	if playerHaveNode {
		lid = playerNode.LayerId
	}
	Layer, _ := GlobalKuangzhanBll.GetLayerItem(lid)
	responseObj.SetResultStatus(Common.ResultStatus_Success)
	responseObj.Data = Layer
	return responseObj
}

//
// UpdateKuangzhanOccupy
// @Description:占领镖局
// @receiver this
// @param playerId:用户id
// @param layerId:层数id
// @param positionId:节点id
// @return *webServer.ResponseObject:响应对象
//
func (this *KuangzhanApi) UpdateKuangzhanOccupy(playerId string, layerId, positionId int32) *webServer.ResponseObject {
	responseObj := webServer.GetInitResponseObj()

	player, playerExists := GlobalPlayerBll.GetItem(playerId)
	if !playerExists {
		return responseObj.SetResultStatus(Common.ResultStatus_PlayerNoExist)
	}

	layer, layerExist := BaseKuangzhanModelBll.GetItem(layerId)
	if !layerExist {
		return responseObj.SetResultStatus(Common.ResultStatus_DataNotExist)
	}
	_, positionExist := layer[positionId]
	if !positionExist {
		return responseObj.SetResultStatus(Common.ResultStatus_DataNotExist)
	}

	//fightingExist := GlobalKuangzhanFightingBll.GetFightingInfo(layerId, positionId)
	//if fightingExist {
	//	return responseObj.SetResultStatus(Common.ResultStatus_Cross_UnchartedPointIsTerritorying)
	//}

	// 查询自己是否已经占领镖局
	playerNode, playerHaveNode := GlobalKuangzhanBll.GetPlayerItem(playerId)

	// 查询将要占领的镖局是否已经被占领
	nodeInfo, havePerson := GlobalKuangzhanBll.GetPositionIsHavePerson(layerId, positionId)
	if nodeInfo.PlayerId == player.Id {
		return responseObj.SetResultStatus(Common.ResultStatus_KuangzhanOccupyNode)
	}

	if havePerson {
		// 该节点有人占领
		fightingExist := GlobalKuangzhanFightingBll.AddFighting(layerId, positionId)
		if fightingExist {
			return responseObj.SetResultStatus(Common.ResultStatus_Cross_UnchartedPointIsTerritorying)
		}
		rand.Seed(time.Now().Unix())
		randNum := rand.Intn(100)
		GlobalKuangzhanFightingBll.DeleteFighting(layerId, positionId)

		if randNum >= 50 {
			return responseObj.SetResultStatus(Common.ResultStatus_FightFail)
		}

		GlobalKuangzhanOccupyBll.DeletePlayerOccupy(nodeInfo.PlayerId)
	}
	playerOccupyNode := GlobalKuangzhanBll.UpdateInfo(layerId, positionId, playerId, player.GuildId)

	GlobalKuangzhanPlayer.DeletePlayer(nodeInfo.PlayerId)
	GlobalKuangzhanPlayer.AddPlayer(playerOccupyNode)

	if playerHaveNode {
		if layerId == playerNode.LayerId && playerNode.GuildId == nodeInfo.GuildId {
			GlobalKuangzhanGuild.ReduceGangTerritoryPlayerCount(player.GuildId, playerNode.LayerId)
		}
		GlobalKuangzhanGuild.AddGangTerritoryPlayerCount(player.GuildId, layerId)
		GlobalKuangzhanGuild.ReduceGangTerritoryPlayerCount(player.GuildId, playerNode.LayerId)
		GlobalKuangzhanBll.UpdateInfo(playerNode.LayerId, playerNode.PositionId, "", "")
	} else {
		GlobalKuangzhanGuild.AddGangTerritoryPlayerCount(player.GuildId, layerId)
	}
	GlobalKuangzhanOccupyBll.UpdatePlayerOccupyInfo(playerId, time.Now())

	return responseObj.SetResultStatus(Common.ResultStatus_Success)
}

//
// GetKuangzhanOverview
// @Description:镖局总览
// @receiver this
// @return *webServer.ResponseObject:响应对象
//
func (this *KuangzhanApi) GetKuangzhanOverview() *webServer.ResponseObject {
	responseObj := webServer.GetInitResponseObj()
	responseObj.Data = GlobalKuangzhanBll.GetKuangzhanGangOverview()
	return responseObj
}

//
// GetKuangzhanGangTerritory
// @Description:查看帮派领地
// @receiver this
// @param playerId:用户id
// @return *webServer.ResponseObject:响应对象
//
func (this *KuangzhanApi) GetKuangzhanGangTerritory(playerId string) *webServer.ResponseObject {
	responseObj := webServer.GetInitResponseObj()

	player, playerExist := GlobalPlayerBll.GetItem(playerId)
	if !playerExist {
		return responseObj.SetResultStatus(Common.ResultStatus_PlayerNoExist)
	}
	guild, guildExist := GlobalGuildBll.GetItem(player.GuildId)
	if !guildExist {
		return responseObj.SetResultStatus(Common.ResultStatus_GuildNotInter)
	}
	GangTerritoryInfo, exist := GlobalKuangzhanBll.GetKuangzhanGangTerritoryInfo(guild.GuildId)
	if !exist {
		return responseObj.SetResultStatus(Common.ResultStatus_GuildNotInter)
	}
	responseObj.Data = GangTerritoryInfo

	return responseObj
}

//
// GetKuangzhanLayer
// @Description:前往帮会成员镖局 / 从镖局总览点击前往镖局 / 翻页
// @receiver this
// @param layerId:层数id
// @return *webServer.ResponseObject:响应对象
//
func (this *KuangzhanApi) GetKuangzhanLayer(layerId int32) *webServer.ResponseObject {
	responseObj := webServer.GetInitResponseObj()
	_, layerExist := BaseKuangzhanModelBll.GetItem(layerId)
	if !layerExist {
		return responseObj.SetResultStatus(Common.ResultStatus_DataNotExist)
	}

	layer, _ := GlobalKuangzhanBll.GetLayerItem(layerId)

	responseObj.Data = layer

	return responseObj
}

//
// ReceiveIncome
// @Description:用户领取收益
// @receiver this
// @param playerId:用户id
// @return *webServer.ResponseObject:响应对象
//
func (this *KuangzhanApi) ReceiveIncome(playerId string) *webServer.ResponseObject {
	responseObj := webServer.GetInitResponseObj()

	player, playerExist := GlobalPlayerBll.GetItem(playerId)
	if !playerExist {
		return responseObj.SetResultStatus(Common.ResultStatus_PlayerNoExist)
	}
	playerIncome := GlobalKuangzhanOccupyBll.ReceivePlayerIncome(player.Id)

	responseObj.Data = playerIncome

	return responseObj
}
